/**
 * 
 */
package sh.t2.share.module.recharge;

import java.util.HashMap;
import java.util.Map;

import sh.t2.share.apiutil.ApiUtil;

/**
 * @author YY
 *
 */
public class RechargeExtra {
	private int productId;
	private String orderId;
	private long money;
	private String openId;
	private long playerId;
	private String sign;
	private int level;
	private int vipLevel;
	
	public static RechargeExtra decode(String extraStr) {
		RechargeExtra extra = new RechargeExtra();
		
		String[] parts = extraStr.split("#");
		for(String part : parts) {
			if("".equals(part)) {
				continue;
			}
			String[] subParts = part.split("=");
			String fieldName = subParts[0];
			String fieldValue = subParts[1];
			if("product".equals(fieldName)) {
				extra.productId = Integer.parseInt(fieldValue);
				
			} else if("order".equals(fieldName)) {
				extra.orderId = fieldValue;
				
			} else if("money".equals(fieldName)) {
				extra.money = Long.parseLong(fieldValue);
				
			} else if("open".equals(fieldName)) {
				extra.openId = fieldValue;
				
			} else if("player".equals(fieldName)) {
				extra.playerId = Long.parseLong(fieldValue);
				
			} else if("sign".equals(fieldName)) {
				extra.sign = fieldValue;
				
			} else if("lv".equals(fieldName)) {
				extra.level = Integer.parseInt(fieldValue);
				
			} else if("vip".equals(fieldName)) {
				extra.vipLevel = Integer.parseInt(fieldValue);
			}
		}
		return extra;
	}
	
	//有些渠道透传参数限制了长度，真尼玛服了
	public String encode() {
		String str = "";
		str += "product=" + productId + "#";
		str += "order=" + orderId + "#";
		str += "money=" + money + "#";
		str += "open=" + openId + "#";
		str += "player=" + playerId + "#";
		str += "sign=" + sign + "#";
		str += "lv=" + level + "#";
		str += "vip=" + vipLevel + "#";
		return str;
	}
	
	public String buildSign(String signKey) {
		Map<String, Object> params = new HashMap<>();
		params.put("productId", String.valueOf(this.productId));
		params.put("orderId", String.valueOf(this.orderId));
		params.put("money", String.valueOf(this.money));
		params.put("openId", String.valueOf(this.openId));
		params.put("playerId", String.valueOf(this.playerId));
        String source = ApiUtil.jointValueToSource(params, signKey);
        return ApiUtil.buildSign(source);
	}
	
	public long getMoney() {
		return money;
	}
	public void setMoney(long money) {
		this.money = money;
	}
	public String getSign() {
		return sign;
	}
	public void setSign(String sign) {
		this.sign = sign;
	}
	public int getProductId() {
		return productId;
	}
	public void setProductId(int productId) {
		this.productId = productId;
	}
	public String getOpenId() {
		return openId;
	}
	public void setOpenId(String openId) {
		this.openId = openId;
	}
	public long getPlayerId() {
		return playerId;
	}
	public void setPlayerId(long playerId) {
		this.playerId = playerId;
	}
	public String getOrderId() {
		return orderId;
	}
	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}

	public int getLevel() {
		return level;
	}

	public void setLevel(int level) {
		this.level = level;
	}

	public int getVipLevel() {
		return vipLevel;
	}

	public void setVipLevel(int vipLevel) {
		this.vipLevel = vipLevel;
	}
}
